<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% css_bundle :learning_outcomes %>
<% js_bundle :rubric_assessment %>
<% content_for :page_title do %><%= t :outcomes_for, "Outcomes for %{user_name}", :user_name => @user.name %><% end %>
  
<% content_for :right_side do %>
  <div class="rs-margin-bottom">
    <% chain = (@context.account_chain rescue [@domain_root_account]).select{|a| can_do(a, @current_user, :manage) }; %>
    <% if !chain.empty? %>
      <%= before_label :see_results, "See %{user_name}'s results for", :user_name => @user.short_name %>
      <ul>
      <% chain.each do |account| %>
        <li><a href="<%= context_url(account, :context_user_outcomes_results_url, @user.id) %>"><%= account.name %></a></li>
      <% end %>
      </ul>
    <% end %>
  </div>
  <div class="show_all_artificts">
    <a href="#" class="btn button-sidebar-wide" id="show_all_artifacts_link"><%= t "#buttons.show_all_artifacts", "Show All Artifacts" %></a>
  </div>
<% end %>

<% content_for :stylesheets do %>
<style>
#outcomes th {
  background-color: #444;
  color: #eee;
  padding: 5px 5px;
  font-size: 1.2em;
  text-align: center;
}
#outcomes th.outcome {
  text-align: left;
}
#outcomes td {
  padding: 2px 5px;
  text-align: center;
  border-bottom: 1px dotted #ccc;
}
#outcomes .short_description {
  text-align: left;
  min-width: 150px;
}
#outcomes .attempts .sub_attempt {
  font-size: 0.8em;
}
#outcomes .latest_score {
  white-space: nowrap;
}
#outcomes .out_of {
  font-size: 0.8em;
}
#outcomes .latest_score.mastery {
  background-color: #efe;
}
#outcomes .latest_score {
  background-color: #fee;
}
#outcomes td#outcome_results {
  text-align: left;
}
</style>
<% end %>
<% js_bundle 'legacy/user_outcome_results' %>

<h1 class="screenreader-only"><%= t :outcomes_for, "Outcomes for %{user_name}", :user_name => @user.name %></h1>

<table id="outcomes">
<thead>
  <tr>
    <th class="outcome"><%= t "short_title.outcome", "Outcome" %></th>
    <th><%= t "short_title.attempts", "Attempts" %></th>
    <th><%= t "short_title.latest_score", "Latest Score" %></th>
    <th><%= t "short_title.average_percent", "Avg Percent" %></th>
</thead>
<tbody>
  <% @outcomes.each do |outcome| %>
    <tr>
      <td class="short_description"><%= outcome.short_description %></td>
      <% results = @results_for_outcome[outcome.id] || [] %>
      <td class="attempts">
        <%= t :attempts_from_artifacts, "%{attempts} *from %{artifacts}*", :wrapper => '<div class="sub_attempt">\1</div>', :attempts => t(:attempts, "attempt", :count => results.map{|r| [1, r.attempt || 0].max }.sum), :artifacts => t(:artifacts, "artifact", :count => results.length) %>
      </td>
      <td class="latest_score <%= 'mastery' if results[-1] && results[-1].mastery %>" title="<%= (results[-1] && results[-1].mastery) ? t(:mastery, "Mastery on latest attempt") : t(:no_mastery, "No Mastery on latest attempt") %>">
        <% if results.empty? %>
           - 
        <% else %>
          <% if results[-1].possible %>
            <%= t :score_out_of_possible, "%{score}* out of %{possible}*", :score => results[-1].score, :possible => results[-1].possible, :wrapper => '<span class="out_of">\1</span>' %>
          <% else %>
            <%= results[-1].score %><span class="out_of"></span>
          <% end %>
        <% end %>
      </td>
      <td class="avg_percent">
        <% results_with_percent = results.select { |r| r.percent.present? }
           if results_with_percent.empty? %>
           - 
        <% else %>
          <%= results_with_percent.map(&:percent).sum.to_f * 100.0 / results.length.to_f %>%
        <% end %>
      </td>
    </tr>
    <% unless results.empty? %>
      <tr class="artifact_details" style="display: none;">
        <td colspan="4" id="outcome_results">
          <ul id="outcome_results_list">
            <% results.each do |result| %>
              {{> views/outcome_result}}
            <% end %>
          </ul>
        </td>
      </tr>
    <% end %>
  <% end %>
  </tbody>
</table>
{{> views/shared/rubric_criterion_dialog}}
